from functools import wraps

from flask import current_app, jsonify
from flask import g
from flask import session

from info.utils.response_code import RET


def hot_news_filter(index):
    if index == 1:
        return 'first'
    elif index == 2:
        return "second"
    elif index == 3:
        return "third"
    else:
        return ""


from info.models import User


# 定义登录装饰器，封装用户的登录信息
def user_login_data(view_function):
    @wraps(view_function)
    def wrapper(*args,**kwargs):
        # 1.从session中取出用户user_id
        user_id = session.get("user_id")
        user = None

        # 2.通过user_id 取出user对象
        if user_id:
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
        # 3. 将user数据封装到g对象
        g.user = user
        return view_function(*args,**kwargs)
    return wrapper